Skip to content

Return Status::Failure from HDF5IO::open instead of throwing - #363

Merged
oruebel merged 3 commits into
mainfrom
hdf5io-open-exceptions
Nov 4, 2026
Merged

oruebel merged 3 commits into
mainfrom
hdf5io-open-exceptions

Conversation

@oruebel

@oruebel oruebel commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #362

Motivation

HDF5IO::open returns a Status, but let H5::Exception escape if HDF5 couldn't open or create the file, e.g., for a file that is not an HDF5 file or a directory that does not exist or is not writable. HDF5 C++ exceptions do not derive from std::exception, so callers that check the returned status (and catch std::exception) did not catch them. In the Open Ephys NWB Format plugin, this terminated the GUI (open-ephys-plugins/nwb-format#6 catches H5::Exception as a workaround).

Changes

  • HDF5IO::open(FileMode, const ChunkCacheConfig&) catches H5::Exception when opening or creating the file, prints the HDF5 error to std::cerr, and returns Status::Failure, as openS3, openRemote, and close already do.
  • An invalid file mode returns Status::Failure instead of throwing std::invalid_argument.
  • HDF5IO::openS3 and HDF5IO::openRemote already caught the exception of the file open, but returned Status::Failure without a message. They now print the HDF5 error too, and a message if the driver can't be set.
  • In all three functions, the file access property list is created inside the try block, since H5::FileAccPropList can throw too.
  • open uses the non-throwing overload of std::filesystem::exists, which otherwise throws std::filesystem::filesystem_error, e.g., if a directory of the path is not accessible.
  • Documented the failure cases of open, openS3, and openRemote.
  • Added tests: opening a non-HDF5 file in ReadWrite and ReadOnly mode, creating a file in a non-existent directory (and closing the IO afterwards), and an invalid file mode. The existing "invalid URL returns failure" tests cover the failure path of openS3 and openRemote.

Testing

  • Local build with the CI warning flags (with ROS3); all 141 tests pass.
  • Local build with -DAQNWB_USE_REMFILE=ON, as in CI; all 143 tests pass, including the ROS3 and remfile tests.
  • clang-format, codespell, and the docs build report no issues.

🤖 Generated with Claude Code

oruebel and others added 2 commits November 4, 2026 00:08
HDF5IO::open let H5::Exception escape if HDF5 couldn't open or create the
file (e.g., a file that is not an HDF5 file, or a directory that does not
exist or is not writable), and threw std::invalid_argument for an invalid
file mode. HDF5 exceptions do not derive from std::exception, so callers
that check the returned status did not catch them. Catch them, print the
HDF5 error, and return Status::Failure.

openS3 and openRemote already caught the exception of the file open, but
returned without a message. They now print the HDF5 error too. In all
three functions, the creation of the file access property list (which
can throw too) is inside the try block. open uses the non-throwing
overload of std::filesystem::exists.

Fixes #362

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oruebel oruebel added this to the 0.6.0 milestone Nov 4, 2026
Comment thread CHANGELOG.md Resolved
@oruebel
oruebel marked this pull request as ready for review November 4, 2026 08:22
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.71%. Comparing base (59a508f) to head (43f02a9).

Files with missing lines Patch % Lines
src/io/hdf5/HDF5IO.cpp 87.50% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #363      +/-   ##
==========================================
+ Coverage   84.69%   84.71%   +0.02%     
==========================================
  Files          71       71              
  Lines        4651     4664      +13     
  Branches      668      668              
==========================================
+ Hits         3939     3951      +12     
- Misses        712      713       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oruebel oruebel left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed. Looks good

@oruebel
oruebel merged commit d5d5c2d into main Nov 4, 2026
21 checks passed
@oruebel
oruebel deleted the hdf5io-open-exceptions branch November 4, 2026 08:53
@oruebel oruebel mentioned this pull request Nov 22, 2026
8 of 11 tasks
@oruebel oruebel self-assigned this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HDF5IO::open throws HDF5 exceptions instead of returning Status::Failure

2 participants